package com.wbsxch.demo04.moxie;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wanghongzhi
 * @create 2018-08-31 下午12:02
 **/

public class MoxieRequestDemo {

    /**
     * 发送请求到魔蝎 风控网关 demo
     * @throws Exception
     */
    public static void demoToRiskGateway()throws Exception{
        /** app_id */
        String appId = "b3be6ee75bdb4c8dacd6af737adbbf91";
        //魔杖
        String method = Method.MagicWand2.getMethod();
        /** 客户私钥 */
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQClDsc9XnaS0QIIko0s0kmJ+cnMf2FBt4siTPBlMPZJn0bGdD6N8ars2aWpeAXs77pEV5tTzbanLcI+AuCl5/sY4cQcUp2MYCMPKoKO4Y3mn/FheeqAHWE9IGtAbz0f3GG8rzYeBvBg/qSBYwKpuzA4I8lgAqPcOaQzoojFikffp2ysYWGG1T7wgB5WmLJZwTKDAQ6w9jgsiTK3uO3U5xYV5Wz7ogRTP4q9um8thx7HCxnKG9OZa4Ntnw6ysOSNXfb2zlFJDfAub1CNm054YyoxudvUMr0nk1Es3EH+GKpPIPbsDLWWv8WM7zkObjgvtJzc+5oJYkE1nWIS1vH+YjfDAgMBAAECggEAMorH777RSDvAB6f4mtT5t8pJRNlsipcNf79XjWTkxb6ECZjm3sg1srkixTX/eoId43eAFLVVTPPyGtL+v0690OyxBIs3uxhlI+u8dswSi6BGZbbTW8+jSvh2XxCxAuSU+RYd2Cv/x3OyI/yRgC/VoBs1OrUUQAdWhIksoHCreYxolpe5lPWx/Cr76E4ay9lwg8tb8YJkpgWe3CXYP6un5gC7E1xCxdBnYG1cJ+A729CaqozqvLLf02TAXl7EqZT7LnqdtgJU5ANN2eJFUEWhZNbxCJm4EpE1eODKLbjRG665NWrhPtZZbhQN1WOU3H9Aq3nVsFPcd931z/4QYXm7YQKBgQDgZ9OsxURe87RUwXYknzTY7T58kT67OU1HcBLnDA6EJ/PpMe0Gzyo+7ADkwXvNyVRXnzABhH9wTFgzHPiwGV5qxXtgKrT65cGDSoat5FdYbz6XKCfDF85UJlVr/OyrMiseBVfPq+LcG9GS6NvXooJWOH9uhh2mC6bA5yA9h0jgBQKBgQC8S+W5fitLbPCgqxoLLY8ZyT9VfkpDij5baeDa4ea1uppL9riI8ecaG9tdJVEFn9G4R4BXpgVgjy43CRn76UmGvV5roA5sJVCnlt1TI37CxwTCw68e2RqPMJg/fs/bipFDPRfoFvR7k4ou+0C0vomeUoEn3L+sAZII7wt0iO5rJwKBgHNUqTN4RwVN1vZgX2m+8Rg0BhNbSpRKhO8cKHradXNyXScSY7FGM34XKPZogkpnGJPX2oJNKHn8oq/SSfWndCEH0brA+DaicmwZl1IzsrhepBpnptfbmk0hTnGrka8Cn6pUMKcovfVFztXkG2idusK5UrTo9L/r6Q3nO2ij6M5JAoGAINBlN186ckmI4acPwdTttEOzCtYYYQ7NcSbRbbOifhcH3qcfX5YwfQdPwjrHlOBWLvQe9KdVFo+9MRAf/BuamaWUt9LL0KLsHFiZN2zfIViHht2NPiX9j+ibD62s0YijagFSkjq13ndGxIs6qoTt/K5KHcVFjdc0OORXrbZTPk8CgYEAlxBRU8k5rYZ/ZJAJJyNpnNDURWiNSan1PINwFUy0WHUhhal6rRnUN2MEVKsA+PIMKdoSA1oMVibYcVbNxf23+4tBQNiCn032Vp2lgjOS778VQQ4hXY3G0xemn0axU1TSFMVazHHajCPcg+4Pq9jKLWd5Qz0JTnFAO77a7a9UUeA=";

        /** 请求参数 */
        Map<String,String> reqParams = new HashMap<>();
        reqParams.put(ReqCommonParams.METHOD,"moxie.api.risk.magicwand2");
        reqParams.put(ReqCommonParams.APP_ID,"b3be6ee75bdb4c8dacd6af737adbbf91");
        reqParams.put(ReqCommonParams.VERSION,"1.0");
        reqParams.put(ReqCommonParams.FORMAT,"JSON");
        reqParams.put(ReqCommonParams.SIGN_TYPE,"RSA");
        reqParams.put(ReqCommonParams.TIMESTAMP,String.valueOf(System.currentTimeMillis()));

        /** 业务参数 */
        Map<String,String> bizParams = new HashMap<>();
        bizParams.put("name","李萌");
        bizParams.put("mobile","18203471721");
        bizParams.put("idcard","142603199312083040");
        String bizContent = new ObjectMapper().writeValueAsString(bizParams);
        System.out.println("bizContent:"+bizContent);
        reqParams.put(ReqCommonParams.BIZ_CONTENT,bizContent);

        //签名
        String sign = MoxieSignUtils.signSHA1WithRSA(reqParams,privateKey);

        reqParams.put(ReqCommonParams.SIGN,sign);
        String url = HttpUtils.getWholeGetURL(API_URL,reqParams);
        System.out.println("url:"+url);
        //String resContent = HttpUtils.get(API_URL,reqParams);

        //System.out.println("resContent:\n"+resContent);
    }

    private final static String API_URL = "https://api.51datakey.com/risk-gateway/api/gateway";

    public enum Method{
        MagicWand2("moxie.api.risk.magicwand2","魔杖2.0"),
        MagicScore("moxie.api.risk.magicscore","魔分"),
        MagiccueTags("moxie.api.risk.magiccube.tags","魔方标签");
        private String method;
        private String desc;
        Method(String method,String desc){
            this.method = method;
            this.desc = desc;
        }
        public String getMethod(){
            return this.method;
        }
    }

    public interface ReqCommonParams{
        String METHOD = "method";
        String APP_ID = "app_id";
        String VERSION = "version";
        String FORMAT = "format";
        String SIGN_TYPE = "sign_type";
        String TIMESTAMP = "timestamp";
        String BIZ_CONTENT = "biz_content";
        String SIGN = "sign";
    }

    public interface ReqCommonParamsValue{
        String VERSION = "1.0";
        String FORMAT = "JSON";
        String SIGN_TYPE = "RSA";
    }

    public static void main(String[] args) throws Exception {
        demoToRiskGateway();
    }

}
